package com.juejin.www.dao;

import com.juejin.www.model.DTO.NoticeDTO;
import com.juejin.www.model.VO.AuditVO;
import com.juejin.www.model.VO.NoticeVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author Jacky
 */
@Mapper
public interface NoticeDao {

    /**
     * 添加通知
     * @param noticeDTO 通知实体
     */
    @Insert("insert into notice(id, content, recipient, is_read, audit_id,create_time) " +
            "values (null,#{content},#{recipient},0,#{auditId},#{createTime})")
    void addNotice(NoticeDTO noticeDTO);

    /**
     * 获取通知列表
     * @param begin 开始索引
     * @param pageSize 查询数目
     * @param recipient 收信人
     * @return 通知列表
     */
    @Select("select * from notice where recipient=#{recipient} order by create_time desc limit #{begin},#{pageSize}")
    List<NoticeVO> getNotices(Integer begin,Integer pageSize,Integer recipient);

    /**
     * 获取通知总数
     * @param recipient 收信人
     * @return 通知总数
     */
    @Select("select count(*) from notice where recipient=#{recipient}")
    Integer getNoticesTotalCount(Integer recipient);

    /**
     * 获取审核结果
     * @param auditId 审核id
     * @return 审核结果
     */
    @Select("select * from audits where id=#{auditId}")
    AuditVO getAuditResult(Integer auditId);

    /**
     * 用户读取通知后修改成已读
     * @param id 通知id
     */
    @Update("update notice set is_read=1 where id=#{id}")
    void updateIsRead(Integer id);

    /**
     * 获取用户未读信息数
     * @param recipient 收信人
     * @return 未读信息数
     */
    @Select("select count(*) from notice where is_read=0 and recipient=#{recipient};")
    Integer getIsNotReadCount(Integer recipient);
}
